define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'message_board/index' + location.search,
                    add_url: 'message_board/add',
                    edit_url: 'message_board/edit',
                    del_url: 'message_board/del',
                    multi_url: 'message_board/multi',
                    import_url: 'message_board/import',
                    table: 'message_board',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                searchFormVisible: false,
                commonSearch: false,
                search:false,
                showToggle: false,
                showColumns: false,
                visible: false,
                // showExport: false,
                fixedColumns: false,
                exportOptions: {
                    fileName: '留言列表_' + Moment().format("YYYY-MM-DD"),  //导出名称
                    ignoreColumn: ['user.avatar'], //不导出字段
                    htmlContent: true,
                    ignoreRow: [],
                    mso:{
                        // fileFormat:        'xlsx',
                        //修复导出数字不显示为科学计数法
                        onMsoNumberFormat: function (cell, row, col) {
                            return !isNaN($(cell).text())?'\\@':'';
                        }
                    },
                    csvSeparator: "----",//文本分割// 处理导出图片
                    onCellHtmlData: function ($cell, row, col, htmlData) {
                        var html = $.parseHTML(htmlData);
                        var inputidx = 0;
                        var selectidx = 0;

                        var result = '';
                        $.each(html, function () {
                            if ($(this).is("input")) {
                                result += $cell.find('input').eq(inputidx++).val();
                            }
                            else if ($(this).is("select")) {
                                result += $cell.find('select option:selected').eq(selectidx++).text();
                            }
                            else if ($(this).is("img")) {
                                var src = $(this).attr('src');
                                if (src.indexOf('http') === -1) {
                                    $(this).attr('src', window.location.origin + src);
                                }
                                $(this).attr("width", "100").attr("height", "100").attr('class', '');
                                result += '<div>' + $(this).get(0).outerHTML + '</div>';
                            } else {
                                if (typeof $(this).html() === 'undefined') {
                                    result += $(this).text();
                                }
                                else if (jQuery().bootstrapTable === undefined ||
                                    ($(this).hasClass('filterControl') !== true &&
                                        $cell.parents('.detail-view').length === 0)) {
                                    result += $(this).html();
                                }
                            }
                        });
                        return result;
                    },
                },
                columns: [
                    [
                        // {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'affiliated_center_text', title: __('Affiliated_center_text'), operate:false, formatter: Table.api.formatter.label},
                        {field: 'user.avatar', title: __('User.avatar'), operate: 'LIKE', events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'user.nickname', title: __('User.nickname'), operate: 'LIKE'},
                        {field: 'name', title: '聯絡人', operate: 'LIKE'},
                        {field: 'mobile', title: __('Mobile'), operate: 'LIKE'},
                        {field: 'content', title: __('Content'), operate: 'LIKE'},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime}
                        // {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            table.on("pre-body.bs.table",function(){
                var bt=table.data("bootstrap.table");
                if (bt){
                    bt.$toolbar.find(".export").find(".icon-share").text("導出");
                    bt.$toolbar.find(".export").find(".icon-share").removeClass("glyphicon glyphicon-export icon-share");
                }
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
         user_list: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'message_board/user_list/ids/'+ Config.ids + location.search,
                    add_url: 'staff/add',
                    edit_url: 'staff/edit',
                    del_url: 'staff/del',
                    multi_url: 'staff/multi',
                    import_url: 'staff/import',
                    table: 'staff',
                }
            });

            var table = $("#table");
            $(".btn-add").data("area", ["100%", "100%"]);
            //当内容渲染完成给编辑按钮添加`data-area`属性
            table.on('post-body.bs.table', function (e, settings, json, xhr) {
                $(".btn-editone").data("area", ["100%", "100%"]);
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                searchFormVisible: false,
                search:false,
                showColumns:false,
                showExport:true,
                commonSearch:false,
                pageSize: 10,
                showToggle: false,
                exportOptions: {
                    fileName: '留言記錄_' + Moment().format("YYYY-MM-DD"),  //导出名称
                    ignoreColumn: ['avatar'], //不导出字段
                    ignoreRow: [],
                    mso:{
                        // fileFormat:        'xlsx',
                        //修复导出数字不显示为科学计数法
                        onMsoNumberFormat: function (cell, row, col) {
                            return !isNaN($(cell).text())?'\\@':'';
                        }
                    },
                    csvSeparator: "----",//文本分割
                },
                columns: [
                    [
                        {checkbox: true},
                   

                        {field: 'createtime', title: __('留言時間'), operate:false, addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                     
                        {field: 'name', title: __('聯絡人'), operate: 'LIKE'},

                        {field: 'mobile', title: __('聯繫方式'), operate: 'LIKE'},

                        {field: 'content', title: __('留言内容'), operate: 'LIKE'},
                    ]
                ]
            });
              table.on("pre-body.bs.table",function(){
                var bt=table.data("bootstrap.table");
                if (bt){
                    bt.$toolbar.find(".export").find(".icon-share").text("導出");
                    bt.$toolbar.find(".export").find(".icon-share").removeClass("glyphicon glyphicon-export icon-share");
                }
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});